home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / NMK / Recorders / TranscriptManager.m < prev   
Text File  |  1995-06-12  |  3KB  |  200 lines

  1. /***** TranscriptManager.m -- recording/archiving object implementation
  2.     NeXTstep Measurement Kit
  3.     by Alex Meyer <ameyer@phoenix.Princeton.EDU>
  4.     for computer science senior thesis
  5.     16 April 1992 -- created
  6.     21 April 1992 -- provided clean interface for cycles of setFile/close
  7. *****/
  8.  
  9. #import <stdlib.h>
  10. #import <string.h>
  11. #import <objc/typedstream.h>
  12. #import <objc/Storage.h>
  13. #import "TranscriptManager.h"
  14.  
  15. @implementation TranscriptManager
  16.  
  17. - init
  18. {
  19.     storage = [Storage alloc];
  20.     [storage initCount:0
  21.         elementSize:sizeof(tmRec)
  22.         description:NULL];
  23.     fileValid = NO;
  24.     return (self);
  25. }
  26.  
  27.  
  28. - free
  29. {
  30.     [self close];
  31.     [storage free];
  32.     [super free];
  33.     return (nil);
  34. }
  35.  
  36.  
  37. - setFile:(NXAtom)fName
  38. {
  39.     fileValid = YES;
  40.     fileName = fName;
  41.     return (self);
  42. }
  43.  
  44.  
  45. - close
  46. {
  47.     unsigned i,num;
  48.  
  49.     num = [storage count];
  50.     for (i = 0;i < num;++i) {
  51.         tmPtr ptr;
  52.  
  53.         ptr = (tmPtr) [storage elementAt:i];
  54.         free(ptr->Stats);
  55.     }
  56.     [storage empty];
  57.     fileValid = NO;
  58.     return (self);
  59. }
  60.  
  61.  
  62. - load
  63. {
  64.     NXTypedStream *stream;
  65.  
  66.     if (!(fileValid))
  67.         return (nil);
  68.     stream = NXOpenTypedStreamForFile(fileName,NX_READONLY);
  69.     if (stream) {
  70.         unsigned i,num;
  71.  
  72.         NXReadType(stream,"i",&num);
  73.         for (i = 0;i < num;++i) {
  74.             tmRec rec;
  75.  
  76.             NXReadType(stream,"i",&rec.type);
  77.             NXReadType(stream,"%",&rec.key);
  78.             NXReadType(stream,"%",&rec.desc);
  79.             NXReadType(stream,"i",&rec.statsSize);
  80.             rec.Stats = malloc(rec.statsSize);
  81.             NXReadType(stream,rec.desc,rec.Stats);
  82.             [storage addElement:&rec];
  83.         }
  84.         NXCloseTypedStream(stream);
  85.         return (self);
  86.     }
  87.     return (nil);
  88. }
  89.  
  90.  
  91. - save
  92. {
  93.     NXTypedStream *stream;
  94.  
  95.     if (!(fileValid))
  96.         return (nil);
  97.     stream = NXOpenTypedStreamForFile(fileName,NX_WRITEONLY);
  98.     if (stream) {
  99.         unsigned i,num;
  100.  
  101.         num = [storage count];
  102.         NXWriteType(stream,"i",&num);
  103.         for (i = 0;i < num;++i) {
  104.             tmPtr ptr;
  105.  
  106.             ptr = (tmPtr) [storage elementAt:i];
  107.             NXWriteType(stream,"i",&ptr->type);
  108.             NXWriteType(stream,"%",&ptr->key);
  109.             NXWriteType(stream,"%",&ptr->desc);
  110.             NXWriteType(stream,"i",&ptr->statsSize);
  111.             NXWriteType(stream,ptr->desc,ptr->Stats);
  112.         }
  113.         NXCloseTypedStream(stream);
  114.         return (self);
  115.     }
  116.     return (nil);
  117. }
  118.  
  119.  
  120. - (unsigned)addRecord:(tmPtr)rec
  121. {
  122.     [storage addElement:rec];
  123.     return ([storage count] - 1);
  124. }
  125.  
  126.  
  127. - (unsigned)numRecords
  128. {
  129.     return ([storage count]);
  130. }
  131.  
  132.  
  133. - copyRecordIndex:(unsigned)ind
  134.     into:(tmPtr)dst;
  135. {
  136.     tmPtr ptr;
  137.  
  138.     ptr = (tmPtr) [storage elementAt:ind];
  139.     memcpy(dst,ptr,sizeof(*dst));
  140.     return (self);
  141. }
  142.  
  143.  
  144. - copyRecordKey:(NXAtom)key
  145.     ofType:(unsigned)type
  146.     into:(tmPtr)dst
  147.     where:(unsigned *)Ind
  148. {
  149.     unsigned i,num;
  150.     tmPtr ptr;
  151.  
  152.     num = [storage count];
  153.     for (i = 0;i < num;++i) {
  154.         ptr = (tmPtr) [storage elementAt:i];
  155.         if ((ptr->key == key) && (ptr->type == type))
  156.             break;
  157.     }
  158.     if (i < num) {
  159.         memcpy(dst,ptr,sizeof(*dst));
  160.         *Ind = i;
  161.         return (self);
  162.     }
  163.     return (nil);
  164. }
  165.  
  166.  
  167. - (BOOL)findIndex:(unsigned *)Ind
  168.     ofKey:(NXAtom)key
  169.     andType:(unsigned)type
  170. {
  171.     unsigned i,num;
  172.     tmPtr ptr;
  173.  
  174.     num = [storage count];
  175.     for (i = 0;i < num;++i) {
  176.         ptr = (tmPtr) [storage elementAt:i];
  177.         if ((ptr->key == key) && (ptr->type == type)) {
  178.             *Ind = i;
  179.             return (YES);
  180.         }
  181.     }
  182.     return (NO);
  183. }
  184.  
  185.  
  186. - copyIndex:(unsigned)ind
  187.     statsFrom:(void *)Stats
  188. {
  189.     tmPtr ptr;
  190.  
  191.     ptr = (tmPtr) [storage elementAt:ind];
  192.     if (ptr->Stats)
  193.         free(ptr->Stats);
  194.     ptr->Stats = malloc(ptr->statsSize);
  195.     memcpy(ptr->Stats,Stats,ptr->statsSize);
  196.     return (self);
  197. }
  198.  
  199. @end
  200.